//
//  SelectView.m
//  预登记系统
//
//  Created by mac on 16/4/11.
//  Copyright © 2016年 Simon. All rights reserved.
//

#import "SelectView.h"

@implementation SelectView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)dealloc {
    
//    NSLog(@"dealloc");
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
        self.keyArr = [NSMutableArray array];
        self.layer.cornerRadius = 8;
        self.clipsToBounds = YES;
    }
    return self;
}

- (void)setCellData:(NSMutableDictionary *)cellData {
    
    _cellData = cellData;
    
    [self _createTableViewNormal];
}

- (void)_createTableViewNormal {
    if (_tableViewCount == 1) {

        _tableView = [[SelectTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth / 2 - 20, kScreenHeight / 3)];
        _tableView.tableViewType = 3;
        _tableView.layer.cornerRadius = 8;
        _tableView.cellData = _cellData[@"数据"];
        //    NSLog(@"%@", _tableView);
        [self addSubview:_tableView];
        
        __weak SelectView *weakSelf = self;
        _tableView.selectFinishBolck = ^(NSString *areasName) {
            [weakSelf.keyArr addObject:areasName];
            if (weakSelf.selectFinishedBlock) {
                weakSelf.selectFinishedBlock(weakSelf.keyArr);
            }
        };
    }
    if (_tableViewCount == 2) {
        _firstTableView = [[SelectTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth / 2 - 20, kScreenHeight / 3)];
        _firstTableView.tableViewType = 1;
        _firstTableView.cellData = _cellData;
        [self addSubview:_firstTableView];
        
        __weak SelectView *weakSelf = self;
        _firstTableView.selectForNextBlock = ^(NSString *areasName) {
            [weakSelf.keyArr addObject:areasName];
            weakSelf.tableView.cellData = weakSelf.cellData[areasName];
            [weakSelf.tableView reloadData];
        };
        
        _tableView = [[SelectTableView alloc] initWithFrame:CGRectMake(kScreenWidth / 2, 0, kScreenWidth / 2 - 20, kScreenHeight / 3)];
        _tableView.tableViewType = 3;
        [self addSubview:_tableView];
        
        _tableView.selectFinishBolck = ^(NSString *pcsName) {
            [weakSelf.keyArr addObject:pcsName];
            if (weakSelf.selectFinishedBlock) {
                weakSelf.selectFinishedBlock(weakSelf.keyArr);
            }
        };
    }
    if (_tableViewCount == 3) {
        CGFloat tableViewWidth = (kScreenWidth - 40) / 3;
        _firstTableView = [[SelectTableView alloc] initWithFrame:CGRectMake(0, 0, tableViewWidth, kScreenHeight / 3)];
        _firstTableView.tableViewType = 1;
        _firstTableView.cellData = _cellData;
        [self addSubview:_firstTableView];
        
        __weak SelectView *weakSelf = self;
        _firstTableView.selectForNextBlock = ^(NSString *proName) {
            [weakSelf.keyArr addObject:proName];
            weakSelf.secondTableView.cellData = weakSelf.cellData[proName];
            [weakSelf.secondTableView reloadData];
        };
        _secondTableView = [[SelectTableView alloc] initWithFrame:CGRectMake(tableViewWidth, 0, tableViewWidth, kScreenHeight / 3)];
        _secondTableView.tableViewType = 2;
//        _secondTableView.cellData = _cellData;
        [self addSubview:_secondTableView];
        
        _secondTableView.selectForNextBlock = ^(NSString * citysName) {
            NSDictionary *citysDic = weakSelf.cellData[weakSelf.keyArr.lastObject];
            [weakSelf.keyArr addObject:citysName];
            weakSelf.tableView.cellData = citysDic[citysName];
            [weakSelf.tableView reloadData];
        };
        
        _tableView = [[SelectTableView alloc] initWithFrame:CGRectMake(tableViewWidth * 2, 0, tableViewWidth, kScreenHeight / 3)];
        _tableView.tableViewType = 3;
        [self addSubview:_tableView];
        
        _tableView.selectFinishBolck = ^(NSString *areasName) {
            [weakSelf.keyArr addObject:areasName];
            if (weakSelf.selectFinishedBlock) {
                weakSelf.selectFinishedBlock(weakSelf.keyArr);
            }
        };
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    NSLog(@"%@ touchs began", [self class]);
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
//    NSLog(@"point is not in selectView");
    if (![self.tableView pointInside:p withEvent:event]) {
        
//        NSLog(@"point is not in tableView");
    }
}

@end
